package cn.gdcp.demo_6;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button btnDialog1;
    private Button btnDialog2;
    private Button btnDialog3;
    private Button btnDialog4;
    private Button btnDialog5;
    private Button btnDialog6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showSimpleDialog();
        showSelectDialog();
        showSimpleSelectDialog();
        showMultipleSelectDialog();
        showCustomDialog();
        showProgressDialog();


    }

    /**
     * 简单对话框
     */
    private void showSimpleDialog() {
        btnDialog1 = findViewById(R.id.btn_dialog1);
        btnDialog1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("提示")
                        .setMessage("这是一个简单的对话框")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "知道了", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "别管它", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int i) {
                                Toast.makeText(MainActivity.this, "无所谓", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    /**
     * 选择对话框
     */
    private void showSelectDialog() {
        btnDialog2 = findViewById(R.id.btn_dialog2);
        btnDialog2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] cities = {"北京", "广州", "上海", "深圳"};
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("请选择城市")
                        .setItems(cities, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int i) {
                                Toast.makeText(MainActivity.this, "你居住在：" + cities[i], Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    /**
     * 单选对话框
     */
    private void showSimpleSelectDialog() {
        btnDialog3 = findViewById(R.id.btn_dialog3);
        btnDialog3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] sex = {"男", "女", "东方不败", "保密"};
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("请选择你的性别")
                        .setSingleChoiceItems(sex, -1, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int i) {
                                Toast.makeText(MainActivity.this, "你的性别是：" + sex[i], Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int i) {
//                                Toast.makeText(MainActivity.this,"你是"+ sex[i],Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();

            }
        });
    }

    /**
     * 多选对话框
     */
    private void showMultipleSelectDialog() {
        btnDialog4 = findViewById(R.id.btn_dialog4);
        btnDialog4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] hobby = {"打篮球", "跑步", "听音乐", "写代码", "弹吉他", "种花", "写书法"};
                final boolean[] isCheckd = new boolean[7];
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("请选择你的爱好")
                        .setMultiChoiceItems(hobby, null, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int i, boolean b) {
                                isCheckd[i] = b;
                                if (b) {
                                    Toast.makeText(MainActivity.this, "你是添加了" + hobby[i], Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(MainActivity.this, "你取消了" + hobby[i], Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int i) {
                                StringBuffer buffer = new StringBuffer();

                                for (int j = 0; j < isCheckd.length; j++) {
                                    if (isCheckd[j]) {
                                        buffer.append(hobby[j]);
                                    }
                                }
                                Toast.makeText(MainActivity.this, "你的爱好有" + buffer, Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    /**
     * 自定义对话框
     */
    private void showCustomDialog() {
        btnDialog5 = findViewById(R.id.btn_dialog5);
        btnDialog5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                final EditText editText1 = new EditText(MainActivity.this);
//                final EditText editText2 = new EditText(MainActivity.this);

                View view = LayoutInflater.from(MainActivity.this)
                        .inflate(R.layout.dialog_input, null, false);

                final EditText nameText = view.findViewById(R.id.edt_name);
                final EditText ageText = view.findViewById(R.id.edt_age);

                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("请输入姓名和年龄")
                        .setView(view)
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int i) {
                                String name = nameText.getText().toString();
                                String age = ageText.getText().toString();
                                Toast.makeText(MainActivity.this, "你的姓名是" + name + "，今年" + age + "岁", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }
        });

    }

    /**
     * 进度条对话框
     */
    private void showProgressDialog() {
        btnDialog6 = findViewById(R.id.btn_dialog6);
        btnDialog6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProgressDialog dialog = new ProgressDialog(MainActivity.this);
                dialog.setTitle("进度条正在玩命加载中");
                dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                dialog.show();

            }
        });

    }

}
